package com.woniu.immunologicalmechanism.service.impl;

import com.woniu.common.entity.ImmuneRegister;
import com.woniu.common.entity.ResponseResult;
import com.woniu.immunologicalmechanism.mapper.ImmuneRegisterMapper;
import com.woniu.immunologicalmechanism.service.ImmuneRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Service
public class ImmuneRegisterServiceImpl implements ImmuneRegisterService {
    @Autowired
    ImmuneRegisterMapper immuneRegisterMapper;

    @Override
    //添加免疫登记
    public ResponseResult addimmuneRegister(ImmuneRegister immuneRegister) {

        ResponseResult result=new ResponseResult();

        if(immuneRegister==null){

           return  ResultUtil.empty(result);
        }

        try {
           // System.out.println(immuneRegister.getImmune_date());
            //日期不存在，生成日期
           if(immuneRegister.getImmune_date()==null||immuneRegister.getImmune_date()==""){

               DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
               String format = dateFormat.format(new Date());
               immuneRegister.setImmune_date(format);
           }

      if (immuneRegister.getImmune_num()!=null&&immuneRegister.getImmune_num()!=""){
          //编号存在，重新生成编号
          if (getimmuneRegisterExists(immuneRegister.getImmune_num())){

              String str = UUID.randomUUID().toString().replace("-", "");
              immuneRegister.setImmune_num(str);
          }

      }else {
          String str = UUID.randomUUID().toString().replace("-", "");
          immuneRegister.setImmune_num(str);
      }

           immuneRegisterMapper.insertimmuneRegister(immuneRegister);
           ResultUtil.insertOk(result);
        }
        catch (Exception e){
            e.printStackTrace();
            ResultUtil.error(result);
        }
        return result;
    }
    //修改免疫登记信息
    @Override
    public ResponseResult updateimmuneRegister(ImmuneRegister immuneRegister) {

        ResponseResult result=new ResponseResult();

        if (getimmuneRegisterExists(immuneRegister.getImmune_num())){
            //编号存在，不执行更改
            result.setMessage("更改失败，免疫证号已存在");
            result.setCode(410);
            System.out.println(result);
            return  result;
        }
        try {
            immuneRegisterMapper.updateimmuneRegister(immuneRegister);
           ResultUtil.updateOk(result);
        }
        catch (Exception e){
            e.printStackTrace();
           ResultUtil.error(result);
        }
        System.out.println(result);
        return result;
    }

    //根据id查询免疫登记信息

    @Override
    public ResponseResult<ImmuneRegister> getimmuneRegister(int iid) {
        ResponseResult result=new ResponseResult();

        try {
            ImmuneRegister immuneRegister = immuneRegisterMapper.selectimmuneRegister(iid);
            ResultUtil.selectOk(result);
            result.setData(immuneRegister);
        }
        catch (Exception e){
            e.printStackTrace();
            ResultUtil.error(result);
        }
        return result;
    }

    //判断免疫证号是否存在
    @Override
    public boolean getimmuneRegisterExists(String number) {
        ImmuneRegister immuneRegister = immuneRegisterMapper.getimmuneRegisterExists(number);
        if (immuneRegister==null){
            return false;
        }

        return true;
    }

    @Override
    public ResponseResult<ImmuneRegister> getimmuneRegisterByOid(int oid) {

        ResponseResult result=new ResponseResult();

        try {
           ;
            result.setData( immuneRegisterMapper.selectimmuneRegisterByOid(oid));
            ResultUtil.selectOk(result);
        }
        catch (Exception e){
            e.printStackTrace();
            ResultUtil.error(result);
        }
        return result;
    }


}
